package com.guiji.quartz.domain;

import cn.hutool.core.date.DateUtil;
import cn.hutool.crypto.SecureUtil;
import cn.hutool.http.HttpException;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.json.JSONUtil;
import cn.wanghaomiao.xpath.exception.NoSuchAxisException;
import cn.wanghaomiao.xpath.exception.NoSuchFunctionException;
import cn.wanghaomiao.xpath.exception.XpathSyntaxErrorException;
import cn.wanghaomiao.xpath.model.JXDocument;
import com.guiji.common.utils.spring.SpringUtils;
import com.guiji.quartz.config.ProxyConfig;
import com.guiji.quartz.formatter.DataTemplateFormatter;
import com.guiji.quartz.service.ISpiderNewsService;
import com.guiji.quartz.service.RedisService;
import com.guiji.quartz.task.DataSaveUtil;
import com.guiji.quartz.util.DateFormatUtil;
import com.guiji.quartz.util.DelPicUtil;
import com.guiji.quartz.util.Enums;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.text.SimpleDateFormat;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @program: cms-vue-plus
 * @description:
 * @author: gaoX
 * @create: 2021-10-18 15:23
 **/
public class NanFangWang {
	private static final Logger log = LoggerFactory.getLogger(NanFangWang.class);

	public void mainMethod() {

		for (Enums.Citys city : Enums.Citys.values()) {
			HttpResponse response = null;
			try {
				response = HttpRequest.get(city.getKey()).
					header("accept-encoding", "gzip, deflate, br")
					.header("Cookie", "HMF_CI=0f6ce03ea60c5d8ff902a5ac8be105650af28965ad25f7aabf17d4424e0dac85d9")
					.setConnectionTimeout(15000)
					.execute();
			} catch (Exception e) {
				continue;
			}
			String resp = response.body();
			//获取子链接
			List<String> listContent = getContentUrl(resp);
			//爬取具体内容
			try {
				getContent(listContent, city.getValue());
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}

	private static List<String> getContentUrl(String content) {
		List<String> res = new ArrayList<>();
		String pattern = "/[0-9]{1,10}/[0-9]{7}.html";
		Pattern p = Pattern.compile(pattern);
		Matcher m = p.matcher(content);
		while (m.find()) {
			res.add("http://pc.nfapp.southcn.com/" + m.group());
		}
		return res;
	}

	private static void getContent(List<String> listContent, String city) throws NoSuchFunctionException, XpathSyntaxErrorException, NoSuchAxisException {
		for (String url : listContent) {
			log.info("getContent执行到-{},listContent长度-{}", url, listContent.size());
			String doc ;
			JXDocument jxDocument = null;

			HttpResponse response = HttpRequest.get(url).setConnectionTimeout(15000).execute();
			doc = response.body();
			jxDocument = new JXDocument(doc);

			String title ;
			try {
				title = (String) jxDocument.sel("//*[@id='content']/h2/text()").get(0);
			} catch (Exception e) {
				log.error("标题错误-------------" + url + "-------------------标题错误");
				continue;
			}

			StringBuffer content = new StringBuffer();
			jxDocument.sel("//div[@class='article-content']//founder-content//p/text()").forEach(s->{
				content.append(s+"$$$");
			});
			if(content.length()==0){
				jxDocument.sel("//div[@class='article-content']//p/text()").forEach(s->{
					content.append(s+"$$$");
				});
			}

			List<Object> newImgs = null;
			try {
				newImgs = jxDocument.sel("//tr[@class='firstRow']//td//img/@src");
			} catch (Exception e) {
				log.error("获取图片失败");
			}

			DataTemplateFormatter dataTemplateFormatter = new DataTemplateFormatter();
			Date date = null;
			try {
				date = dataTemplateFormatter.format( jxDocument.sel("//p[@class='article-info']//span/text()").get(0)+":00");
			} catch (Exception e) {
				log.error("获取时间失败");
				date = new Date();
			}

			if (content.length() > 5000 || content.toString().replaceAll("\\$|[\\s\\p{Zs}]","").length() < 20 || newImgs.size() > 20) {
				continue;
			}
			try {
				DataSaveUtil.saveData(title, content.toString(), newImgs, url, date, "http://www.southcn.com/", "南方网-"+city);
				log.info(Thread.currentThread().getName() +  "-----------------南方网-{}----------------",city);
			} catch (Exception e) {
				continue;
			}
		}
	}

}
